var opttype = "new";// new=新增,edit=修改，delete=删除,show=查看,reload=刷新
var url = "save"
$(document).ready(
		function() {
			// 新增
			$("#newbtn").click(function() {
				onNewHandler();
			});
			// 修改
			$("#editbtn").click(function() {
				onEditHandler();
			});
			// 删除
			$("#deletebtn").click(function() {
				onDeleteHandler();
			});
			// 刷新
			$("#reloadbtn").click(function() {
				$("#tdg").treegrid("reload");
				$('#tdg').treegrid("unselectAll");
			});
			// 折叠
			$("#foldbtn").click(
				function() {
					$("#tdg").treegrid("collapseAll",
							$("#tdg").treegrid("getRoot").target);
			});
			// 展开
			$("#unfoldbtn").click(
				function() {
					$("#tdg").treegrid("expandAll",
							$("#tdg").treegrid("getRoot").target);
			});
			// 提交
			$("#dlg-savebtn").click(function() {
				onSubmit();
			});
			// 关闭弹出窗口
			$("#dlg-closebtn").click(function() {
				$("#dlg").dialog("close");
			});

			// 表格双击
			$("#tdg").treegrid({
				"onDblClickRow" : function(row) {
					onShowHandler(row);
				}
			});

		});

// 新增
function onNewHandler() {
	
	$("#dataform").form("clear");
	$("#dlg").dialog("setTitle", "新增");
	$("#dlg").dialog("open");
	opttype = "save";
	$("#iid").val(0);
	$("#dlg-savebtn").linkbutton("enable");
	$("#dlg-savebtn").linkbutton({
		"text" : "保存"
	});
	try {
		if (subNewHandler && typeof (subNewHandler) == "function") {
			subNewHandler();
		}
	} catch (e) {

	}
	$("#dlg-savebtn").show();
}
// 修改
function onEditHandler() {
	var rows = $("#tdg").treegrid("getSelections");
	if (rows == null || rows.length == 0) {
		$.messager.alert("提示", "请选择后，再操作！");
		return;
	}
	if (rows.length != 1) {
		$.messager.alert("提示", "只能选择一条数据进行修改！");
		return;
	}

	// 清空表单
	$("#dataform").form("clear");
	var row = rows[0];
	$("#dlg").dialog("setTitle", "编辑");
	$("#dlg").dialog("open");
	opttype = "edit";
	$("#dlg-savebtn").linkbutton("enable");
	$("#dlg-savebtn").linkbutton({
		"text" : "修改"
	});
	try {
		if (subEditHandler && typeof (subEditHandler) == "function") {
			subEditHandler(row);
		}
	} catch (e) {

	}
	$("#dataform").form("load", row);
	$("#dlg-savebtn").show();
}

// 查看
function onShowHandler(row) {
	// 清空表单
	$("#dataform").form("clear");
	$("#dataform").form("load", row);
	$("#dlg").dialog("setTitle", "查看");
	$("#dlg").dialog("open");
	opttype = "show";
	try {
		if (subEditHandler && typeof (subEditHandler) == "function") {
			subEditHandler(row);
		}
	} catch (e) {

	}
	$("#dlg-savebtn").hide();
}

// 删除
function onDeleteHandler() {
	var rows = $("#tdg").treegrid("getSelections");
	if (rows == null || rows.length == 0) {
		$.messager.alert("提示", "请选择后，再操作！");
		return;
	}
	$.messager.confirm("提示", "是否删除选择项?", function(issure) {
		if (issure) {
			var ids = Common.getIds(rows, "iid");
			var nodes = $("#tdg").treegrid("getChildren", ids);
			if (nodes = !null && nodes.length != 0) {
				$.messager.alert("提示", "不能删除有子节点的项！");
			} else {
				submitDeleteHandler(ids);
			}
		}
	});
}

// 提交删除操作
function submitDeleteHandler(ids) {
	Common.ajax("removeByIds", {
		"ids" : ids
	}, function(data) {
		Common.showAlert("", data.msg, "info")
		$('#tdg').treegrid("unselectAll");
		$('#tdg').treegrid('reload');
	}, null);
}

// 提交
function onSubmit() {

	$("#dlg-savebtn").linkbutton("disable");
	if (opttype == "save") {
		url = "save";
	} else {
		url = "edit";
	}

	try {
		if (subSubmitHandler && typeof (subSubmitHandler) == "function") {
			subSubmitHandler();
		}
	} catch (e) {

	}

	$("#dataform").form("submit", {
		url : url,
		dataType : "json",
		onSubmit : function() {
			//Common.showProgress("正在请求中...");
			var bool = $("#dataform").form("validate");
			if (bool == false) {
				$("#dlg-savebtn").linkbutton("enable");
				Common.closeProgress();
			}
			return bool;
		},
		error : function(XMLHttpRequest, textStatus, errorThrow) {
			Common.closeProgress();
			Common.showAlert("请求错误", XMLHttpRequest.responseText, "error");
			$("#dlg-savebtn").linkbutton("enable");
		},
		success : function(data, textStatus) {
			Common.closeProgress();
			var answerJson = eval('(' + data + ')');
			$("#dlg-savebtn").linkbutton("enable");
			Common.showAlert("提示", answerJson.msg, "info");
			if (answerJson.success) {
				$("#dataform").form("clear");
				$("#dlg").dialog("close");
				$("#tdg").treegrid("unselectAll");
				$("#tdg").treegrid("reload");
			}
		}
	});
}
